package pzl.com.threadrelated;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import pzl.com.threadrelated.tasks.SubThread1;
import pzl.com.threadrelated.tasks.SubThread2;
import pzl.com.threadrelated.tasks.SubThread3;

public class NotifyMainThreadActivity extends AppCompatActivity {
    TextView tvShow1;
    static TextView tvShow2;
    TextView tvShow3;

    Handler myHandler3 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("pzl", "handleMessage start 当前线程：" + Thread.currentThread().getName());
            tvShow3.setText((CharSequence) msg.obj);
            Log.e("pzl", "handleMessage end 当前线程：" + Thread.currentThread().getName());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_main_thread);

        tvShow1 = (TextView) findViewById(R.id.tvShow1);
        tvShow2 = (TextView) findViewById(R.id.tvShow2);
        tvShow3 = (TextView) findViewById(R.id.tvShow3);

    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.btnNotify1:
                new SubThread1(tvShow1).start();
                break;
            case R.id.btnNotify2:
                new SubThread2().start();
                break;
            case R.id.btnNotify3:
                new SubThread3(myHandler3).start();
                break;
        }
    }

    public static class MyHandler2 extends Handler {

        public MyHandler2(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("pzl", "handleMessage start 当前线程：" + Thread.currentThread().getName());
            tvShow2.setText((CharSequence) msg.obj);
            Log.e("pzl", "handleMessage end 当前线程：" + Thread.currentThread().getName());
        }
    }

}
